/*
 * AdapterBase.h
 *
 *  Created on: Nov 10, 2012
 *      Author: Scorpion
 */

#ifndef AdapterBase_H_
#define AdapterBase_H_
#include <errno.h>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream>
#include <vector>
#include <sstream>
#include <assert.h>
#include <map>
using namespace std;
namespace centny {
namespace file {
#ifdef WIN32
#define F_SEQ "\\"
#else
#define F_SEQ "/"
#endif
//
const int MONTHES_SIZE = 12;
extern const char * MONTHES[MONTHES_SIZE];
//
class AdapterBase;
class NetAdapterBase;
//
class FInfo {
public:
	char type;
	unsigned long long size;
	time_t mtime;
	string name;
	string cwd;
	AdapterBase *cb;
	FInfo *parent;
protected:
	vector<FInfo*> _subs;
public:
	FInfo(AdapterBase *cb, FInfo *parent = 0);
	virtual ~FInfo();
	//
	virtual vector<FInfo*>& subs();
	virtual void showInfo();
	virtual void showAll();
	virtual bool isDir();
	virtual FInfo* mkdir(string name);
	//the subclass must inherit fre() function.
	//it will be used to free this object.
	virtual void fre();
	//
	virtual string absUrl();
};
//
class NetFInfo: public FInfo {
public:
	NetFInfo(NetAdapterBase *cb, NetFInfo *parent = 0);
	virtual const char* upload(string lf, string name);
	virtual const char* upload(istream& is, string name);
	virtual const char* download(string of);
	virtual const char* download(ostream& os);
};
//
class AdapterBase {
protected:
	string rurl;
protected:
	map<string, FInfo*> cwd2f;
public:
	AdapterBase();
	virtual ~AdapterBase();
	//path=="/" is meaning get the root FInfo.
	virtual FInfo* finfo(string path = F_SEQ);
	//create the root FInfo by root URL.
	//it must free all old FInfo* in cwd2f map by calling fre().
	virtual FInfo* root()=0;
	//free all FInfo.
	virtual void fre();
	//it will auto call FInfo.fre() to free
	//all added object when adapter is freed.
	virtual void addToMap(FInfo* fi);
	//
	virtual string rootUrl();
	//
	virtual vector<FInfo*> subs(FInfo* parent)=0;
	virtual FInfo* mkdir(FInfo* fi, string name)=0;
	virtual string absUrl(FInfo* tg)=0;

};
//
class NetAdapterBase: public AdapterBase {
public:
	string usrpwd;
public:
	//virtual vector<FInfo*> convert(FInfo* parent, stringstream& buf);
	//virtual FInfo* convertOne(FInfo* parent, string line);
	virtual const char* upload(FInfo* fi, string lf, string name)=0;
	virtual const char* upload(FInfo* fi, istream& is, string name)=0;
	virtual const char* download(FInfo* fi, string lf)=0;
	virtual const char* download(FInfo* fi, ostream& os)=0;
	virtual string absUrl(FInfo* tg);
};
}
} /* namespace centny */
#endif /* AdapterBase_H_ */
